設置詳見 之前的 Laravel-Octane
參考:
找到 vendor/laravel/octane/bin/createSwooleServer.php
,並修改 第6行 。把 Http 改為 WebSocket:
6 $server = new Swoole\Http\Server(
6 $server = new Swoole\WebSocket\Server(
找到 vendor/laravel/octane/bin/swoole-server
,在 62 行後加入以下片段:
/* Socket */
$server->on("start", function (Swoole\WebSocket\Server $server) {
echo "Socket Server";
});
$server->on("open", function (Swoole\WebSocket\Server $server, $request) {
echo "server: handshake success with fd{$request->fd}\n";
});
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
echo "編號:{$frame->fd},訊息:{$frame->data}\n";
$server->push($frame->fd, "this is server");
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
執行指令不變
php artisan octane:start
啟動後 socket 會跑在
ws://127.0.0.1:8000